﻿using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;

namespace Common
{
    public class AliyunMessage
    {
        string osssecretid = "LTAI4G9JGJ8bG4XofWrMPCwz";
        string osssecretkey = "w6MduBWH8zlaJv1zYQ94xlUXL5kaHu";

        //通过阿里云短信发送验证码
        public bool SendMessageByAliyun(string PhoneNum, string validatacode)
        {
            #region 阿里云短信平台
            //获取当前站点的阿里云信息配置
            validatacode = "{\"code\":\"" + validatacode + "\"}";
            return sendMessage(PhoneNum, validatacode, osssecretid, osssecretkey, "时刻记", "SMS_188480117");
            #endregion
        }

        public bool sendMessage(string userphone, string code, string accessKeyId, string accessKeySecret, string signname, string tempcode)
        {
            bool isOk = false;
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", userphone);
            request.AddQueryParameters("SignName", signname);
            request.AddQueryParameters("TemplateCode", tempcode);
            request.AddQueryParameters("TemplateParam", code);
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                isOk = true;
                //if(response.)
            }
            catch (ServerException e)
            {
                isOk = false;
            }
            catch (ClientException e)
            {
                isOk = false;
            }
            return isOk;
        }

        public string GetValidataCode()
        {
            string ValidataCode = string.Empty;//所得6位验证码
            Random rand = new Random();
            string str = rand.Next(100000, 999999) + "";
            return str.Replace('4', '8');
        }
    }
}
